package org.microblog.service.impl;

import org.apache.ibatis.session.SqlSession;
import org.microblog.dao.BlogDao;
import org.microblog.dao.CommentDao;
import org.microblog.dao.impl.BlogDaoImpl;
import org.microblog.dao.impl.CommentDaoImpl;
import org.microblog.entity.Blog;
import org.microblog.entity.Comment;
import org.microblog.service.CommentService;
import org.microblog.util.DatabaseUtil;
import org.microblog.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Service("CommentService")
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentDao commentDao;

    public void setCommentDao(CommentDao commentDao) {
        this.commentDao = commentDao;
    }

    @Override
    public int getCommentCountByBid(int bid) throws SQLException {
        //SqlSession sqlSession=sessionFactory.openSession();
       // CommentDao dao=sqlSession.getMapper(CommentDao.class);
        int ret=commentDao.getCommentCountByBid(bid);
        //sqlSession.commit();
        //sqlSession.close();
        return ret;
    }

    @Override
    public List<Comment> getCommentsByBid(int bid) throws SQLException {
        //SqlSession sqlSession=sessionFactory.openSession(true);
        //CommentDao dao=sqlSession.getMapper(CommentDao.class);
        //sqlSession.commit();
       // sqlSession.close();
        List<Comment> list=commentDao.getCommentsByBid(bid);
        return list;
    }

    @Override
    public int addComment(Comment comment) throws SQLException {
        //SqlSession sqlSession=sessionFactory.openSession();
        //CommentDao dao=sqlSession.getMapper(CommentDao.class);
        int ret=commentDao.addComment(comment);
        //sqlSession.commit();
       // sqlSession.close();
        return ret;
    }

    @Override
    public int deleteCommentsByBid(int bid) throws SQLException {
        //SqlSession sqlSession=sessionFactory.openSession();
        //CommentDao dao=sqlSession.getMapper(CommentDao.class);
        int ret=commentDao.deleteCommentsByBid(bid);
        //sqlSession.commit();
        //sqlSession.close();
        return ret;
    }

    @Override
    public void getCommentsByPage(int bid, Page pageObj) throws SQLException {
        //Connection conn = null;
        try{
            //conn = DatabaseUtil.getConnection();
            //CommentDao commentDao = new CommentDaoImpl(conn);
            int totalCount = commentDao.getCommentCountByBid(bid);
            pageObj.setTotalCount(totalCount); // 设置总数量并计算总页数
            if (totalCount > 0) {
                // 对末页进行控制
                if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount())
                    pageObj.setCurrPageNo(pageObj.getTotalPageCount());
                // 进行分页查询
                List<Comment> commentsList = commentDao.getCommentsByPage(bid,pageObj.getCurrPageNo(),pageObj.getPageSize());
                pageObj.setCommentList(commentsList);
            } else {
                pageObj.setCurrPageNo(0);
                pageObj.setCommentList(new ArrayList<Comment>());
            }
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
           // DatabaseUtil.closeAll(conn,null,null);
        }
    }

}
